package com.mlnx.work.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<>();

    public static SimpleDateFormat SDF_DAY() {
        SimpleDateFormat sdf;
        sdf = threadLocal.get();
        if (sdf == null) {
            sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        }
        return sdf;
    }

    /**
     * 计算秒差
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Integer getDiffSeconds(Date date1, Date date2) {
        return Math.abs((int) (date1.getTime() - date2.getTime()) / 1000);
    }

    /**
     * 计算分钟差
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Integer getDiffMinutes(Date date1, Date date2) {
        return Math.abs((int) (date1.getTime() - date2.getTime()) / 1000 / 60);
    }

    /**
     * 返回小时
     *
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 计算加上指定分钟后的时间
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Date plusMinutes(Date date, Integer minutes) {
        return new Date(date.getTime() + minutes * 60 * 1000);
    }

    /**
     * 获取当前日期所在星期的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = calendar.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }

        // 设置一个星期的第一天，星期一
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - day);

        try {
            return SDF_DAY().parse(SDF_DAY().format(calendar.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前日期所在月的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.set(Calendar.DAY_OF_MONTH, 1);

        try {
            return SDF_DAY().parse(SDF_DAY().format(calendar.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 比较两个日期前后，按天计算
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int dateCompare(Long date1, Long date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(date2);
        if (calendar1.get(Calendar.YEAR) < calendar2.get(Calendar.YEAR)) {
            return -1;
        } else if (calendar1.get(Calendar.YEAR) > calendar2.get(Calendar.YEAR)) {
            return 1;
        } else {
            if (calendar1.get(Calendar.MONTH) < calendar2.get(Calendar.MONTH)) {
                return -1;
            } else if (calendar1.get(Calendar.MONTH) > calendar2.get(Calendar.MONTH)) {
                return 1;
            } else {
                if (calendar1.get(Calendar.DAY_OF_MONTH) < calendar2.get(Calendar.DAY_OF_MONTH)) {
                    return -1;
                } else if (calendar1.get(Calendar.DAY_OF_MONTH) > calendar2.get(Calendar.DAY_OF_MONTH)) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
    }

    /**
     * 计算两个日期天数差
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Float dayBetween(Long date1, Long date2) {
        return (float) Math.abs((date1 - date2) / (1000 * 3600 * 24));
    }
}
